"""
线程的使用
"""

import threading
import time

t_list = []

# 实例一个锁对象
metax_lock = threading.Lock()


def task1(a, b):
    # 获取当前线程对象
    t = threading.current_thread()
    print(t.name)
    metax_lock.acquire()
    for x in range(a + b):
        t_list.append('TaskA ' + str(x + 1))
        # print('Task A  ', x + 1)
        # print('TaskA', t_list)
        time.sleep(0.5)
    # print(num_1)
    metax_lock.release()
    print('T1 = ' + str(t_list))


def task2(count):
    for x in range(count):
        # print('Task B  ', x + 1)
        t_list.append('TaskB' + str(3 * x))
        # print('TaskB', t_list)
        time.sleep(0.5)
    print('T2 = ' + str(t_list))


if __name__ == '__main__':
    # 创建线程对象
    t1 = threading.Thread(target=task1, name='T1', args=(3, 2))
    t2 = threading.Thread(target=task2, name='T2', kwargs={'count': 5})

    # 启动线程

    t1.start()

    # join表示让t1执行完成后才会执行后面的线程
    # t1.join()
    t2.start()

    # print('name', threading.current_thread().name)
    # print(t1)
    # print(t2)
    time.sleep(4)
    print('Final = ', t_list)
